package com.loubii.sunshineSchool.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loubii.sunshineSchool.FinalVirable;
import com.loubii.sunshineSchool.R;
import com.loubii.sunshineSchool.httpRequest.HttpBin;
import com.loubii.sunshineSchool.pojo.ActivityContainer;
import com.loubii.sunshineSchool.pojo.ActivityItem;
import com.loubii.sunshineSchool.constants.CardRes;
import com.loubii.sunshineSchool.ui.card.SSDetailActivity;
import com.loubii.sunshineSchool.util.ImageUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

public class SSCardAdapter extends BaseRecycleAdapter {


    public static final int TYPE_END = 1;
    public static final int TYPE_ITEM = 0;
    private static int TYPE = TYPE_ITEM;
    private List<ActivityItem> activityList = new ArrayList<>();
    private Boolean hasNext = false;

    private Context context;


    public SSCardAdapter(Context context) {
        this.context = context;
    }

    public void setData(ActivityContainer container) {
        this.activityList = container.activityList;
        this.hasNext = container.hasNext;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM)
            return new ItemHoleder(LayoutInflater.from(context).inflate(R.layout.item_card, parent, false));
        else if (viewType == TYPE_END)
            return new EndHoleder(LayoutInflater.from(context).inflate(R.layout.next_page, parent, false));
        else
            return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
        switch (getItemViewType(position)) {
            case TYPE_ITEM:
                ItemHoleder itemHoleder = (ItemHoleder) holder;

                String type = FinalVirable.types.get(activityList.get(position).getType());
                itemHoleder.mTvBandcardName.setText(activityList.get(position).getUserName());
                itemHoleder.mTvBandcardType.setText(type);
                itemHoleder.mTvBandcardId.setText(activityList.get(position).getTitle());
                itemHoleder.mTvDone.setVisibility(View.VISIBLE);
                itemHoleder.mTvTransfer.setVisibility(View.VISIBLE);
                if (activityList.get(position).getState() != 1)
                    itemHoleder.mTvDone.setVisibility(View.GONE);
                if (activityList.get(position).getState() != 0)
                    itemHoleder.mTvTransfer.setVisibility(View.GONE);
                itemHoleder.itemView.setBackgroundResource(CardRes.getCardBg(type));
                ImageUtil.downloadAvatar(activityList.get(position).getImage(), itemHoleder.mIvBandcard);

                itemHoleder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, SSDetailActivity.class);
                        intent.putExtra("detail", activityList.get(position));
                        context.startActivity(intent);
                    }
                });

                itemHoleder.mTvTransfer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final EditText editText = new EditText(context);
                        editText.setHint("100字以内哦~");
                        editText.setHeight(300);
                        AlertDialog.Builder inputDialog =
                                new AlertDialog.Builder(context);
                        inputDialog.setTitle("请输入理由~").setView(editText);
                        inputDialog.setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        HttpBin.sign(context, activityList.get(position).getBid(), editText.getText().toString(), new HttpBin.ResponseListener<Boolean>() {
                                            @Override
                                            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                                                if (response.body()) {
                                                    Toast.makeText(context,
                                                            "报名成功啦~",
                                                            Toast.LENGTH_LONG).show();
                                                } else {
                                                    Toast.makeText(context,
                                                            "你已经报名啦，不用重复报名哦~",
                                                            Toast.LENGTH_LONG).show();
                                                }
                                            }

                                            @Override
                                            public void onFailure(Call<Boolean> call, Throwable t) {

                                            }
                                        });
                                    }
                                }).show();
                    }
                });
                break;
            default:
                break;
        }
    }


    @Override
    public int getItemCount() {
        return hasNext ? activityList.size() + 1 : activityList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == activityList.size()) {
            return TYPE_END;
        }
        return TYPE_ITEM;
    }


    class ItemHoleder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_bandcard)
        ImageView mIvBandcard;
        @BindView(R.id.tv_bandcard_name)
        TextView mTvBandcardName;
        @BindView(R.id.tv_bandcard_type)
        TextView mTvBandcardType;
        @BindView(R.id.tv_bandcard_id)
        TextView mTvBandcardId;
        @BindView(R.id.tv_transfer)
        TextView mTvTransfer;
        @BindView(R.id.tv_done)
        TextView mTvDone;

        public ItemHoleder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class EndHoleder extends RecyclerView.ViewHolder {
        public EndHoleder(View itemView) {
            super(itemView);
        }
    }

}
